package servlet;

import com.alibaba.fastjson.JSON;
import domain.ResultInfo;
import domain.User;
import mapper.UserMapper;
import utils.MapperUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user/zhuce")
public class ZhuceServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String money = request.getParameter("money");
        User user = new User();

        user.setUsername(username);
        user.setPassword(password);
        user.setMoney(Double.parseDouble(money));
        UserMapper userMapper = MapperUtil.getMapper(UserMapper.class);
        int zhuce = userMapper.Zhuce(user);
        ResultInfo resultInfo = new ResultInfo();
        if(zhuce>0){
            resultInfo.setStatus(1);
            resultInfo.setMsg("注册成功");
            resultInfo.setData(zhuce);
        }else{
            resultInfo.setStatus(0);
            resultInfo.setMsg("注册失败");
        }
        response.getWriter().write(JSON.toJSONString(resultInfo));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}